<template>
  <div class="container">
    <el-card>
      <el-row type="flex" justify="space-between">
        <el-col>
          <!-- 表单 -->
          <el-form class="form" :inline="true" :model="page" :rules="rules">
            <el-form-item label="关键字" prop="keyword">
              <el-input
                placeholder="根据文章标题搜索"
                v-model="page.keyword"
              ></el-input>
            </el-form-item>

            <el-form-item label="状态" prop="state">
              <el-select v-model="page.state">
                <el-option label="启用" value="qiyong"></el-option>
                <el-option label="禁用" value="jinyong"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button size="small" @click="btnRemove">清除</el-button>
              <el-button size="small" type="primary" @click="btnSerch"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <!-- 新增技巧按钮 -->
        <el-col :span="6" style="text-align: right">
          <el-button size="small" type="success" @click="showDialog = true"
            ><i class="el-icon-edit" style="padding-right: 5px"></i
            >新增技巧</el-button
          >
        </el-col>
      </el-row>
      <!-- 提示信息 -->
      <el-row>
        <el-alert type="info" :closable="false">
          <slot label="title"
            ><i class="el-icon-info"></i> 数据一共{{ page.counts }}条
          </slot>
        </el-alert>
      </el-row>
      <el-row>
        <!-- 文章列表 -->
        <el-table :data="list" class="table" v-loading="loading">
          <el-table-column label="序号" type="index"> </el-table-column>
          <el-table-column label="文章标题" width="400px">
            <template slot-scope="{ row }">
              {{ row.title }}
              <i @click="redioFn(row)" class="el-icon-film" v-if="row.videoURL"></i>
            </template>
          </el-table-column>
          <el-table-column label="阅读数" prop="visits" width="80px">
          </el-table-column>
          <el-table-column label="录入人" prop="username" width="80px">
          </el-table-column>
          <el-table-column label="录入时间" width="100px">
            <template slot-scope="{ row }">
              {{ row.createTime | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="state">
            <template slot-scope="{ row }">
              {{ row.state === 1 ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button type="text" size="small" @click="btnPreview(row)"
                >预览</el-button
              >
              <el-button type="text" size="small" @click="btnDisabled(row)">{{
                row.state === 1 ? "禁用" : "启用"
              }}</el-button>
              <el-button
                type="text"
                size="small"
                @click="updateArticle(row)"
                :disabled="row.state === 0"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="delArticle(row)"
                :disabled="row.state === 0"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <!-- 放置分页组件 -->
      <el-row type="flex" justify="end" style="height: 60px">
        <el-pagination
          layout="prev,pager,next,sizes,jumper"
          background
          :current-page="page.page"
          :page-size="page.pagesize"
          :page-sizes="[5, 10, 20, 50]"
          :total="page.counts"
          @current-change="changePage"
          @size-change="changeSize"
        >
        </el-pagination>
      </el-row>
      <!-- 新增技巧弹层 -->
      <add-article
        ref="addArticle"
        :showDialog.sync="showDialog"
        @addArticle="getArticleList"
      ></add-article>

      <!-- 预览文章组件 -->
      <ArticlePreview
        ref="articlePreview"
        :showArticle.sync="showArticle"
      ></ArticlePreview>

      <!-- 视频弹层 -->
      <div v-if="showVideo" class="showVideo">
        <div id="close" @click.prevent="btnClose"><i class="el-icon-close"></i></div>
        <div class="video">
          <video autoplay :src="videoURL"></video>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/articles.js'
import AddArticle from '../components/articles-add.vue'
import ArticlePreview from '../components/articles-preview.vue'
export default {
  components: {
    AddArticle,
    ArticlePreview
  },
  data () {
    return {
      rules: {},
      list: [],
      page: {
        page: 1, // 当前页
        pagesize: 5, // 每页多少条数据
        counts: 0, // 总共多少条数据
        keyword: '', // 关键词
        state: '' // 状态
      },
      loading: false,
      showDialog: false, // 表单弹层
      disabled: false, // 禁用
      showArticle: false,
      videoURL: null,
      showVideo: false, // 视频弹层
      formData: {
        keyword: '', // 关键词
        state: '' // 状态
      }
    }
  },

  created () {
    this.getArticleList()
  },
  methods: {
    // 获取数据列表
    async getArticleList () {
      console.log('获取数据')
      this.loading = true
      const { data } = await list(this.page)
      this.list = data.items
      this.page.counts = data.counts
      this.loading = false
    },

    // 切换页面
    changePage (newPage) {
      this.page.page = newPage
      this.getArticleList()
    },
    // 改变每页条数
    changeSize (newPage) {
      this.page.pagesize = newPage
      this.getArticleList()
    },

    // 删除文章
    async delArticle (row) {
      try {
        await this.$confirm('确定要删除么?')
        await remove(row)
        this.getArticleList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    },

    // 清除
    btnRemove () {
      this.page.keyword = ''
      this.page.state = ''
    },
    // 搜索
    async btnSerch () {
      this.page.page = 1
      this.getArticleList()
    },
    // 修改
    async updateArticle (row) {
      this.$refs.addArticle.updateArticle(row)
      this.showDialog = true
    },
    // 禁用
    async btnDisabled (row) {
      await changeState({
        id: row.id,
        state: row.state
      })
      this.$message.success('操作成功')
      row.state = row.state === 1 ? 0 : 1
    },
    // 预览
    btnPreview (row) {
      this.$refs.articlePreview.btnLook(row)
      this.showArticle = true
    },
    // 播放视频
    redioFn (row) {
      this.videoURL = row.videoURL
      this.showVideo = true
    },
    // 取消播放视频
    btnClose () {
      this.showVideo = false
      this.videoURL = null
    }

  }
}
</script>

<style scoped lang='scss'>
.container {
  margin: 10px;
}
.table {
  margin-top: 20px;
}

.form {
  padding-left: 30px;
}
.el-icon-info {
  font-size: 16px;
}
.el-icon-film {
  color: blue;
}

.center {
  margin-left: 15px;
}
.title {
  margin-top: 20px;
}
.articleContent {
  background-color: #f5f5f5;
  border-top: 1px solid #f5f5f5;
  margin-top: 10px;
}

.showVideo {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
  overflow: hidden;
  background: rgba(0, 0, 0, 0.4);
}
#close {
  position: absolute;
  z-index: 999;
  left: 50%;
  top: 30px;
  transform: translate(-50%, 0);
  width: 50px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  border-radius: 50%;
  color: white;
  background: rgba(0, 0, 0, 0.4);
  i{
    height: 100%;
    width: 100%;
  }
}
.video {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 800px;
  height: 600px;
  video{
    width: 100%;
    height: 100%;
  }
}
</style>
